/**
 * 死锁
 */
public class Deadlock {

    private static Object obj1 = new Object();
    private static Object obj2 = new Object();

    public static void main(String[] args) {
        MyThread myThread1 = new MyThread("线程一");
        MyThread myThread2 = new MyThread("线程二");

        myThread1.start();
        myThread2.start();
    }

    static class MyThread extends Thread{
        @Override
        public void run() {
            String name = getName();
            if (name.equals("线程一")){
                synchronized (obj1){
                    System.out.println("线程一拿到obj1");
                    synchronized (obj2){
                        System.out.println("线程一拿到obj2");
                    }
                }
            }
            else{
                synchronized (obj2){
                    System.out.println("线程二拿到obj2");
                    synchronized (obj1){
                        System.out.println("线程二拿到obj1");
                    }
                }
            }
        }

        public MyThread(String name) {
            super(name);
        }
    }
}
